package cc.flyflow.web.controller.sys;

import cc.flyflow.biz.security.captcha.EasyCaptchaService;
import cc.flyflow.biz.service.ILoginService;
import cc.flyflow.biz.vo.AutoLoginVO;
import cc.flyflow.biz.vo.CaptchaResult;
import cc.flyflow.biz.vo.UserBizVO;
import cc.flyflow.common.config.NotWriteLogAnno;
import cc.flyflow.common.dto.LoginUrlDto;
import cc.flyflow.common.dto.R;
import cn.dev33.satoken.stp.SaTokenInfo;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 用户接口
 *
 * @author Jyl
 */
@Slf4j
@RestController
@RequestMapping(value = {"login"})
public class LoginController
{

    @Resource
    private ILoginService loginService;

    @Resource
    private EasyCaptchaService captchaService;

    /**
     * 获取验证码
     *
     * @return
     */
    @GetMapping("/captcha")
    @NotWriteLogAnno(printResultLog = false)
    public R<CaptchaResult> getCaptcha()
    {
        return captchaService.getCaptcha();
    }

    /**
     * 用户登录
     *
     * @param user
     * @return
     */
    @SneakyThrows
    @PostMapping("login")
    public R login(@RequestBody UserBizVO user)
    {
        return loginService.login(user);
    }

    /**
     * h5用户登录
     *
     * @param user
     * @return
     */
    @SneakyThrows
    @PostMapping("loginAtH5")
    public R<SaTokenInfo> loginAtH5(@RequestBody UserBizVO user)
    {
        return loginService.loginAtH5(user);
    }

    /**
     * 用户token自动登录
     *
     * @param token
     * @return
     */
    @SneakyThrows
    @GetMapping("loginByToken")
    public R<SaTokenInfo> loginByToken(String token)
    {
        return loginService.loginByToken(token);
    }

    /**
     * 用户退出登录
     *
     * @return
     */
    @SneakyThrows
    @PostMapping("logout")
    public R logout()
    {
        return loginService.logout();
    }

    /**
     * 根据不同平台获取登录地址，设计网页登录、钉钉登录
     *
     * @return
     */
    @GetMapping("/getLoginUrl")
    public R<LoginUrlDto> getLoginUrl()
    {
        return loginService.getLoginUrl();
    }

    /**
     * 获取登录参数
     *
     * @return
     */
    @PostMapping("/getLoginParam")
    public R getLoginParam(@RequestBody Map<String, Object> paramMap)
    {
        return loginService.getLoginParam(paramMap);
    }

    /**
     * 自动登录 第三方系统自动传递登录token和用户id登录
     *
     * @param autoLoginVO 登录参数
     * @return
     */
    @PostMapping("auto")
    public R loginAuto(@RequestBody AutoLoginVO autoLoginVO)
    {
        return loginService.loginAuto(autoLoginVO);
    }

    /**
     * 自动登录 第三方系统自动传递登录token和用户id登录
     *
     * @param autoLoginVO 登录参数
     * @return
     */
    @PostMapping("refreshToken")
    public R refreshToken(@RequestBody AutoLoginVO autoLoginVO)
    {
        log.info("[refreshToken]:[]", autoLoginVO.getToken());
        return R.success();
    }

}
